package com.osmino.contacts.list;

import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.util.SparseIntArray;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SectionIndexer;
import android.widget.TextView;
import com.osmino.contacts.R;
import com.osmino.contacts.entity.Contact;
import com.osmino.contacts.sideindex.SectionController;

public final class ContactsSectionAdapter extends ContactsBaseAdapter implements SectionIndexer {

	private static final int TYPE_INFO = 0;
	private static final int TYPE_SECTION = 1;
	private final SectionController mSectionController;
	private final SparseIntArray mTypesArray;
	private final SparseIntArray mSectionForPosition, mPositionForSection;
	private Object[] mSections;

	public ContactsSectionAdapter(Context context, int from, SectionController sectionController) {
		super(context, from);
		mSectionController = sectionController;
		mTypesArray = new SparseIntArray();
		mSectionForPosition = new SparseIntArray();
		mPositionForSection = new SparseIntArray();
		mSections = new Object[0];
	}

	@Override
	public int getPositionForSection(int sectionIndex) {
		return mPositionForSection.get(sectionIndex);
	}

	@Override
	public int getSectionForPosition(int position) {
		return mSectionForPosition.get(position);
	}

	@Override
	public Object[] getSections() {
		return mSections;
	}

	@Override
	public boolean areAllItemsEnabled() {
		return false;
	}
	
	@Override
	public boolean isEnabled(int position) {
		return mTypesArray.get(position) == TYPE_INFO;
	}

	@Override
	public int getViewTypeCount() {
		return 2;
	}

	@Override
	public int getItemViewType(int position) {
		return mTypesArray.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		int viewType = getItemViewType(position);
		View item = null;
		if (viewType == TYPE_INFO) {
			item = super.getView(position, convertView, parent);
		} else if (viewType == TYPE_SECTION) {
			int section = getSectionForPosition(position);
			if (convertView == null) {
				item = newViewForTypeSection(section);
			} else {
				item = convertView;
			}
			bindViewForTypeSection(item, section);
		}
		return item;
	}

	private View newViewForTypeSection(int section) {
		TextView item = new TextView(mContext);
		if (mFrom == FROM_GROUPS) {
			item.setPadding(20, 0, 0, 0);
		}
		return item;
	}
	
	@Override
	protected void bindView(int position, View view) {
		super.bindView(position, view);
		ContactItem contactItem = (ContactItem) view;
		contactItem.setBackground(((position-getSectionForPosition(position)) % 2) != 0);
	}
	
	private void bindViewForTypeSection(View view, int section) {
		TextView textItem = (TextView) view;
		textItem.setBackgroundResource(R.drawable.base_list_header_letter_bg);
		textItem.setGravity(Gravity.CLIP_VERTICAL|Gravity.LEFT);
		textItem.setTextColor(Color.WHITE);
		textItem.setText(String.valueOf(getSections()[section]));
	}

	@Override
	protected void refreshListContactInfo(List<Contact> listContact) {
		mTypesArray.clear();
		mListItems.clear();
		mSectionForPosition.clear();
		mPositionForSection.clear();
		Character currentFirstLetter = null;
		int position = -1;
		int section = -1;
		List<Object> sections = new ArrayList<Object>();
		for (Contact contact : listContact) {
			Character firstLetter = contact.displayName.charAt(0);
			if (firstLetter != currentFirstLetter) {
				currentFirstLetter = firstLetter;
				position++;
				section++;
				mTypesArray.put(position, TYPE_SECTION);
				mSectionForPosition.put(position, section);
				mPositionForSection.put(section, position);
				mListItems.add(firstLetter);
				sections.add(firstLetter);
			}
			position++;
			mTypesArray.put(position, TYPE_INFO);
			mSectionForPosition.put(position, section);
			mListItems.add(contact);
		}
		mSections = sections.toArray();
		notifyDataSetChanged();
		mSectionController.onAdapterContactsChanged(listContact);
	}
	
}
